<template>
  <div class="article">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item to="articleList">目录</el-breadcrumb-item>
      <el-breadcrumb-item>文章详情</el-breadcrumb-item>
    </el-breadcrumb>
    <h1 class="article_title">{{ article.title }}</h1>
    <div class="article_small_title">
      <span>
        标签：
        <el-tag v-for="tag in article.desb" :key="this" class="article_desb">{{
          tag
        }}</el-tag>
      </span>
      <span>时间：{{ article.create_time | timeFormat }}</span>
    </div>
    <div class="article_content" v-html="article.content"></div>

    <Comments :comments="comments"></Comments>
    <div>
      <el-form ref="form" :model="form" label-width="80px">
        <h1>我要发表看法</h1>
        <el-form-item label="您的大名">
          <el-input placeholder="必填" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="电子邮件">
          <el-input placeholder="必填，不公开" v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="您的留言 ">
          <el-input
            type="textarea"
            placeholder="HTML标签部分可用"
            v-model="form.comments"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="isRemember"> 记住个人信息?</el-checkbox>
          <el-button @click="onSubmit" type="primary">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Comments from "@/components/comments/Comments.vue";
import "@/assets/css/quill-edit.css";
import { getUrlKey, timeFormat } from "@/utils/utils.js";
export default {
  data() {
    return {
      articleId: "",
      article: "article",
      form: {
        name: "",
        email: "",
        comments: "",
      },
      comments: {},
      isRemember: false,
    };
  },
  components: {
    Comments,
  },
  mounted() {
    const articleId = getUrlKey("articleId", window.location.href);
    this.articleId = articleId;
    this.getArticle(articleId);
    this.getComments(articleId);
  },

  methods: {
    //获取文章
    async getArticle(articleId) {
      const { data: res } = await this.$http.get(`article/list/${articleId}`);
      this.article = res.data;
      this.article.desb = this.article.desb.split(" ");
    },
    //获取评论
    async getComments(articleId) {
      const { data: res } = await this.$http.get(
        `article/getCommentsList/${articleId}`
      );
      console.log(res);
      this.comments = res.data.filter((item) => item.isShow);
    },
    // 添加评论
    async onSubmit() {
      const { form } = this;
      if (!form.name) return this.$message.error("请输入大名");
      if (!form.email) return this.$message.error("请输入邮箱");
      if (!form.comments) return this.$message.error("请输入留言");
      const res = await this.$http.post(
        `article/getCommentsList/${this.articleId}`,
        form
      );
      if (res.data.meta.status >= 300) return this.$message.error("提交失败");
      this.$message.success("提交成功");
      this.$alert("评论内容审核通过后即可展示", "提交成功", {
        confirmButtonText: "确定",
      });
      this.form={
        name:"",
        email:'',
        comments:""
      }
      this.getComments(this.articleId);
    },
  },
  watch: {
    // 路由发生改变
    $route() {
      const articleId = getUrlKey("articleId", window.location.href);
      console.log("articleId:" + articleId);
      this.getArticle(articleId);
    },
  },

  created() {},
  filters: {
    timeFormat,
  },
};
</script>

<style lang="less" scope>
.article {
  .article_title {
    margin-top: 20px;
    text-align: center;
  }
  .article_small_title {
    text-align: center;
    margin-top: 20px;
    color: #666;
    .article_desb {
      margin-right: 20px;
    }
  }
  .article_content {
    line-height: 30px;
  }
}
.article {
  .article_title {
    text-align: center;
  }
  .article_small_title {
    text-align: center;
    margin-top: 20px;
    color: #666;
    .article_desb {
      margin-right: 20px;
    }
  }
  .article_content {
    line-height: 30px;
  }
}
</style>
